/*   Copyright (C) 2008  Jeroen v. Schagen & Youri Lima
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections;

using NHibernate;

namespace Database
{
    public class ObjectManager<T>
    {
        public static T Get(Object id)
        {
            try
            {
                // Open session
                ISession session = Database.Factory.OpenSession();

                // Retrieve object from database
                T obj = (T)session.Load(typeof(T), id);

                // Close session
                session.Close();

                // Return object
                return obj;
            }

            catch (Exception)
            {
                return default(T);
            }
        }

        public static ArrayList Get()
        {
            ArrayList collection = new ArrayList();

            try
            {
                // Open session
                ISession session = Database.Factory.OpenSession();

                // Retrieve object list from database
                collection = (ArrayList) session.CreateCriteria(typeof(T)).List();

                // Close session
                session.Close();
            }

            catch (Exception)
            {
                
            }

            return collection;
        }

        public static bool Insert(T obj)
        {
            try
            {
                // Open session and transaction
                ISession session = Database.Factory.OpenSession();
                ITransaction transaction = session.BeginTransaction();

                // Save object in session
                session.Save(obj);

                // Commit changes
                transaction.Commit();

                // Close session
                session.Close();

                return true;
            }

            catch (Exception)
            {
                return false;
            }
        }

        public static bool Update(T obj)
        {
            try
            {
                // Open session and transaction
                ISession session = Database.Factory.OpenSession();
                ITransaction transaction = session.BeginTransaction();

                // Save object in session
                session.Update(obj);

                // Commit changes
                transaction.Commit();

                // Close session
                session.Close();

                return true;
            }

            catch (Exception)
            {
                return false;
            }
        }

        public static bool Delete(T obj)
        {
            try
            {
                // Open session and transaction
                ISession session = Database.Factory.OpenSession();
                ITransaction transaction = session.BeginTransaction();

                // Save object in session
                session.Delete(obj);

                // Commit changes
                transaction.Commit();

                // Close session
                session.Close();

                return true;
            }

            catch (Exception)
            {
                return false;
            }
        }
    }
}
